<?php
class NoticeService {

    /**
     * @var NoticeService
     */
    private static $instance;

    /**
     * Get the instance of NoticeService
     * @return NoticeService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new NoticeService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    public function __construct() {

    }

    /**
     * 获取公告配置
     * @param $userId
     * @param $reqTime
     * @return array
     */
    public function getNoticeInfo($userId,$reqTime) {
        $res = array(
            'code' => 0,
            'notice' => array()
        );

        $noticeInfo = array();
        //获取公告配置
        $noticeConfig = SzConfig::get()->loadAppConfig(GameConst::CONFIG_NOTICE, null, true);
        if (empty($noticeConfig) || !$noticeConfig || $noticeConfig == '' || $noticeConfig == null){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        if ($platform == 'hago'){
            $platformPre = 'hago';
        }
        elseif ($platform == 'H5WeChat' || $platform == 'dev'){
            $platformPre = 'weixin';
        }
        else{
            $platformPre = 'wanba';
        }

        $num = 0;
        foreach ($noticeConfig as $key=>$data){
            if ($reqTime >= strtotime($data[$platformPre.'StartTime']) && $reqTime <= strtotime($data[$platformPre.'EndTime'])){
                $num = $key;
                $noticeInfo[] = $data;
            }
        }

        if (isset($noticeConfig[$num - 2])){
            $noticeInfo[] = $noticeConfig[$num - 2];
        }
        if (isset($noticeConfig[$num - 1])){
            $noticeInfo[] = $noticeConfig[$num - 1];
        }

        $res['notice'] = $noticeInfo;
        return $res;
    }



}